package mysql

import (
	"errors"
	"gitee.com/DonHz/qimi/bluebell/models"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

func CreateArticle(art *models.Article) error {
	return db.Create(art).Error
}

func GetArticleById(aid int64) (*models.Article, error) {
	var data models.Article
	err:=db.Select("article_id,author_id, community_id, create_time, update_time, status, title, content").
		Where("article_id=?", aid).Find(&data).Error
	if err!=nil {
		zap.L().Error("通过帖子Id从数据库查找帖子失败",
			zap.Int("id", int(aid)),
			zap.Error(err))
		if err==gorm.ErrRecordNotFound {
			return &models.Article{}, errors.New("无效的author_id")
		}
		return &models.Article{}, err
	}
	return &data, nil
}

func GetArticleList() ([]models.Article, error) {
	var data []models.Article
	err:=db.Select("article_id,author_id, community_id, create_time, update_time, status, title, content").
		Find(&data).Error
	if err!=nil {
		zap.L().Error("获取帖子列表失败",
			zap.Error(err))
		if err==gorm.ErrRecordNotFound {
			return []models.Article{}, errors.New("无效的author_id")
		}
		return []models.Article{}, err
	}
	return data, nil
}